package com.example.othree.service.impl;

import com.example.othree.common.DataUtil;
import com.example.othree.entity.Lidar;
import com.example.othree.mapper.LidarMapper;
import com.example.othree.service.LidarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LidarServiceImpl implements LidarService {
    @Autowired
    LidarMapper lidarMapper;

    @Autowired
    DataUtil dataUtil;

    @Override
    public Map toFindByStationsDay(String station, String yaosu, String startTime, String endTime) {

        List<Lidar> lidarList = lidarMapper.selectDayAverage(startTime, endTime, station, yaosu);
        ArrayList<Lidar> lidarDTOpbl = lidarMapper.selectDayAveragePbl(startTime, endTime, station);

        return toFindStations(lidarList, lidarDTOpbl, yaosu);
    }

    @Override
    public Map toFindByStationsMouth(String station, String yaosu, String startTime, String endTime) {
        List<Lidar> lidarList = lidarMapper.selectMonthAverage(startTime, endTime, station, yaosu);
        ArrayList<Lidar> lidarDTOpbl = lidarMapper.selectMonthAveragePbl(startTime, endTime, station);

        return toFindStations(lidarList, lidarDTOpbl, yaosu);
    }

    @Override
    public Map toFindByStationsHour(String station, String yaosu, String startTime, String endTime) {
        List<Lidar> lidarList = lidarMapper.selectHourAverage(startTime, endTime, station, yaosu);
        ArrayList<Lidar> lidarDTOpbl = lidarMapper.selectHourAveragePbl(startTime, endTime, station);
        System.out.println(lidarDTOpbl);

        return toFindStations(lidarList, lidarDTOpbl, yaosu);
    }

    @Override
    public Map toFindByStationsMin(String station, String yaosu, String startTime, String endTime) {
        List<Lidar> lidarList = lidarMapper.selectMinAverage(startTime, endTime, station, yaosu);
        ArrayList<Lidar> lidarDTOpbl = lidarMapper.selectMinAveragePbl(startTime, endTime, station);

        return toFindStations(lidarList, lidarDTOpbl, yaosu);
    }

    @Override
    public Map toFindStations(List<Lidar> lidarList, ArrayList<Lidar> lidarDTOpbl, String yaosu) {

        //从数据库获得数据
        Map<String, ArrayList<ArrayList<Double>>> lidarDTO = new TreeMap<>();
        for(Lidar lidar: lidarList){
            if (lidar.getExtino3() == null) continue;
            if(lidarDTO.containsKey(lidar.getDatetime())){
                lidarDTO.get(lidar.getDatetime()).add(dataUtil.toList(lidar.getExtino3()));
            }else {
                ArrayList<ArrayList<Double>> doubleList = new ArrayList<>();
                doubleList.add(dataUtil.toList(lidar.getExtino3()));
                lidarDTO.put(lidar.getDatetime(),doubleList);
            }
        }

        ArrayList<String> xData = new ArrayList<>();
        ArrayList<Double> yData = new ArrayList<>();
        ArrayList<ArrayList> zData = new ArrayList<>();

        // 遍历HashMap
        for (Map.Entry<String, ArrayList<ArrayList<Double>>> entry : lidarDTO.entrySet()) {
            // 需要修改xData的格式 -tao Add
//            String dateStr = entry.getKey();
//            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
//            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//            String outputDateStr = null;
//            try {
//                Date date = inputFormat.parse(dateStr);
//                outputDateStr = outputFormat.format(date);
//            } catch (ParseException e) {
//                System.out.println("日期格式错误: " + e.getMessage());
//            }
//            xData.add(outputDateStr);
            //10.31修改，更新xData没数据的问题
            xData.add(entry.getKey());
            // 更新value
            ArrayList<Double> zDataY = dataUtil.calculateAverages(entry.getValue());
            if (yData.isEmpty()) {
                for (int i = 0; i < zDataY.size(); i++) {
                    yData.add(i * 7.5);
                }
            }
            int y = 0;
            for (Double itme : zDataY) {
                // zData要求double类型并保留两位小数 - tao modify
//                ArrayList<String> z = new ArrayList<>();
//                z.add(String.valueOf(xData.size() - 1));
//                z.add(String.valueOf(y));
//                z.add(String.valueOf(itme));
//                zData.add(z);
                ArrayList<Double> z = new ArrayList<>();
                z.add(Math.round(Double.parseDouble(String.valueOf(xData.size() - 1)) * 100.0) / 100.0);
                z.add(Math.round(Double.parseDouble(String.valueOf(y)) * 100.0) / 100.0);
                z.add(Math.round(Double.parseDouble(String.valueOf(itme)) * 100.0) / 100.0);
                zData.add(z);
                y++;
            }
        }

        ArrayList<Double> bjcData = new ArrayList<>();
        for(Lidar l: lidarDTOpbl){
//            bjcData.add(l.getPbl());
            // bjf两位小数
            bjcData.add(Math.round(l.getPbl() * 100.0) / 100.0);

        }

        Map<String, Object> dataReturn = new HashMap<>();
        dataReturn.put("xData", xData);
        dataReturn.put("yData", yData);
        dataReturn.put("zData", zData);
        dataReturn.put("bjcData", bjcData);

        return dataReturn;
    }
}
